﻿using AutoMapper;
using BasicData.Application.Input;
using BasicData.Application.Input.Material;
using BasicData.Application.Output;
using BasicData.Domain.Entities;
using BasicData.Infrastructure.Repositories;
using BasicData.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace BasicData.Application.Services.Material
{
    public class MaterialServices : IMaterialServices
    {
        IBasicModelRepository<MaterialModel> _repository;
        IMapper _mapper;

        public MaterialServices(IBasicModelRepository<MaterialModel> repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
        }

        public ApiResult<int> AddMaterial(MaterialInput input)
        {
            var material = _mapper.Map<MaterialModel>(input);
            material.Id = YitIdHelper.NextId();
            material.CreatedAt = DateTime.Now;
            material.CreatedBy = "wyj";
            _repository.Create(material);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "添加成功",
                Data = 1
            };
        }

        public ApiResult<int> DeleteMaterial(long id)
        {
            ApiResult<int> res = new ApiResult<int>();
            var model = _repository.GetValue().FirstOrDefault(x => x.Id == id);
            if (model == null)
            {
                res.Code = ApiEnums.Fail;
                res.Message = "未找到该数据";
                return res;
            }
            model.IsDeleted = true;
            int i = _repository.Update(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "删除成功";
                res.Data = i;
            }
            else
            {
                res.Code = ApiEnums.Error;
                res.Message = "删除失败";
                res.Data = i;
            }
            return res;
        }

        public ApiPaging<MaterialOutput> GetMaterial(PageInput input)
        {
            ApiPaging<MaterialOutput> res = new ApiPaging<MaterialOutput>();
            var materials = _repository.GetValue().Where(x => x.IsDeleted == false);
            var query = materials.OrderByDescending(x => x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            var list = _mapper.Map<List<MaterialOutput>>(query);
            return new ApiPaging<MaterialOutput>()
            {
                PageDatas = list,
                TotalCount = materials.Count(),
                Code = ApiEnums.OK,
                Message = "获取成功",

            };
        }

        public ApiResult<int> UpdateMaterial(UpdateMaterialInput input)
        {
            var modedl = _mapper.Map<MaterialModel>(input);
            modedl.UpdatedAt = DateTime.Now;
            modedl.CreatedBy = "wyj";
            _repository.Update(modedl);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "修改成功",

            };
        }
    }
}
